package ru.mirent.libs.matcher;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;

public class ListMatcherTest {
    private static final Map<String, String> mapRegexpAndName = new LinkedHashMap<>();

    @BeforeAll
    static void setUp() {
        mapRegexpAndName.put("(^https://)(lotomile)(\\d)(.blogspot.com/)(api/v2/autorization)", "Сервис авторизации v2");
        mapRegexpAndName.put("(^https://)(lotomile.blogspot.com/)(api/v3/autorization)", "Сервис авторизации v3");
        mapRegexpAndName.put("(^https://)(lotomile.blogspot.com/)(api/v3/getprofile)", "Сервис получения профиля");
        mapRegexpAndName.put("(^https://)(lotomile.blogspot.com/)(api/v3/update)", "Сервис обновления данных пользователя");
    }

    @ParameterizedTest
    @ArgumentsSource(TestData.class)
    void matchTextTest(String actual, Object expect) {
        ListMatcher listMatcher = new ListMatcher(mapRegexpAndName);

        Assertions.assertEquals(expect, listMatcher.apply(actual));
    }

    @Test
    void failFindTest() {
        String notFindText = "https://lotomile.blogspot.com";
        ListMatcher listMatcher = new ListMatcher(mapRegexpAndName);

        Assertions.assertNull(listMatcher.apply(notFindText));
    }

    @Test
    void emptyMapRegexpAndNameTest() {
        String notFindText = "https://lotomile.blogspot.com";
        ListMatcher listMatcher = new ListMatcher();

        Assertions.assertNull(listMatcher.apply(notFindText));
    }

    private static class TestData implements ArgumentsProvider {
        @Override
        public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
            return Stream.of(
                    Arguments.of("https://lotomile.blogspot.com/api/v3/update", "Сервис обновления данных пользователя"),
                    Arguments.of("https://lotomile.blogspot.com/api/v3/getprofile", "Сервис получения профиля"),
                    Arguments.of("https://lotomile2.blogspot.com/api/v2/autorization", "Сервис авторизации v2"),
                    Arguments.of("https://lotomile.blogspot.com/api/v3/autorization", "Сервис авторизации v3")
            );
        }
    }
}
